﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TryTools
{
    /// <summary>
    /// 文件夹工具类
    /// </summary>
    public static class DirUtil
    {
        /// <summary>
        /// 删除文件名包含指定字符的文件
        /// </summary>
        /// <param name="dir">文件夹路径</param>
        /// <param name="mark">匹配字符</param>
        /// <returns></returns>
        public static List<string> DelFilesWithExtension(string dir, string mark)
        {
            List<string> result = new List<string>();

            if (Directory.Exists(dir) && !string.IsNullOrEmpty(mark))
            {
                DirectoryInfo dirInfo = new DirectoryInfo(dir);
                FileInfo[] fileInfos = dirInfo.GetFiles(".", SearchOption.AllDirectories);
                foreach (var f in fileInfos)
                {
                    if (Path.GetFileNameWithoutExtension(f.FullName).Contains(mark))
                    {
                        File.Delete(f.FullName);
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// 删除指定后缀名的文件
        /// </summary>
        /// <param name="dir">文件夹路径</param>
        /// <param name="extension">文件后缀名</param>
        /// <returns></returns>
        public static List<string> DelFilesWithMark(string dir, string extension)
        {
            List<string> result = new List<string>();

            if (Directory.Exists(dir) && !string.IsNullOrEmpty(extension))
            {
                DirectoryInfo dirInfo = new DirectoryInfo(dir);
                FileInfo[] fileInfos = dirInfo.GetFiles(".", SearchOption.AllDirectories);
                foreach (var f in fileInfos)
                {
                    if (Path.GetExtension(f.FullName).Replace(".", "").ToLower().Equals(extension.Replace(".", "").ToLower()))
                    {
                        File.Delete(f.FullName);
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// 删除超过n天的文件
        /// </summary>
        /// <param name="dir">文件夹路径</param>
        /// <param name="days">超过的天数</param>
        /// <returns></returns>
        public static List<string> DelFilesOverdue(string dir, int days)
        {
            List<string> result = new List<string>();

            if (Directory.Exists(dir))
            {
                DirectoryInfo dirInfo = new DirectoryInfo(dir);
                FileInfo[] fileInfos = dirInfo.GetFiles(".", SearchOption.AllDirectories);
                foreach (var f in fileInfos)
                {
                    int overDays = (DateTime.Now - f.CreationTimeUtc).Days;
                    if (overDays >= days)
                    {
                        File.Delete(f.FullName);
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// 获取指定后缀名的文件的全路径
        /// </summary>
        /// <param name="dir">文件夹路径</param>
        /// <param name="extension">文件后缀名</param>
        /// <returns></returns>
        public static List<string> GetFilesWithExtension(string dir, string[] extensions)
        {
            List<string> result = new List<string>();

            if (Directory.Exists(dir) && extensions != null && extensions.Length > 0)
            {
                DirectoryInfo dirInfo = new DirectoryInfo(dir);
                FileInfo[] fileInfos = dirInfo.GetFiles(".", SearchOption.AllDirectories);
                foreach (var f in fileInfos)
                {
                    foreach (var extension in extensions)
                    {
                        if (Path.GetExtension(f.FullName).Replace(".", "").ToLower().Equals(extension.Replace(".", "").ToLower()))
                            result.Add(f.FullName);
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// 获取指定后缀名的文件的全路径
        /// </summary>
        /// <param name="dir">文件夹路径</param>
        /// <param name="extension">文件后缀名</param>
        /// <returns></returns>
        public static List<string> GetFilesWithExtension(string dir, string extension)
        {
            List<string> result = new List<string>();

            if (Directory.Exists(dir) && !string.IsNullOrEmpty(extension))
            {
                DirectoryInfo dirInfo = new DirectoryInfo(dir);
                FileInfo[] fileInfos = dirInfo.GetFiles(".", SearchOption.AllDirectories);
                foreach (var f in fileInfos)
                {
                    if (Path.GetExtension(f.FullName).Replace(".","").ToLower().Equals(extension.Replace(".", "").ToLower()))
                        result.Add(f.FullName);
                }
            }

            return result;
        }

        /// <summary>
        /// 获取文件名中包含匹配字符的文件的全路径
        /// </summary>
        /// <param name="dir">文件夹路径</param>
        /// <param name="mark">匹配字符</param>
        /// <returns></returns>
        public static List<string> GetFilesWithMark(string dir, string mark)
        {
            List<string> result = new List<string>();

            if (Directory.Exists(dir) && mark != null)
            {
                DirectoryInfo dirInfo = new DirectoryInfo(dir);
                FileInfo[] fileInfos = dirInfo.GetFiles(".", SearchOption.AllDirectories);
                foreach (var f in fileInfos)
                {
                    if(Path.GetFileNameWithoutExtension(f.FullName).Contains(mark))
                        result.Add(f.FullName);
                }
            }

            return result;
        }

        /// <summary>
        /// 获取所有文件的全路径
        /// </summary>
        /// <param name="dir">文件夹路径</param>
        /// <returns></returns>
        public static List<string> GetFileFullName(string dir)
        {
            List<string> result = new List<string>();

            if (Directory.Exists(dir))
            {
                DirectoryInfo dirInfo = new DirectoryInfo(dir);
                FileInfo[] fileInfos = dirInfo.GetFiles(".", SearchOption.AllDirectories);
                foreach (var f in fileInfos)
                {
                    result.Add(f.FullName);
                }
            }

            return result;
        }

        /// <summary>
        /// 获取所有文件的文件名
        /// </summary>
        /// <param name="dir">文件夹路径</param>
        /// <returns></returns>
        public static List<string> GetFileName(string dir)
        {
            List<string> result = new List<string>();

            if (Directory.Exists(dir))
            {
                DirectoryInfo dirInfo = new DirectoryInfo(dir);
                FileInfo[] fileInfos = dirInfo.GetFiles(".", SearchOption.AllDirectories);
                foreach (var f in fileInfos)
                {
                    result.Add(f.Name);
                }
            }

            return result;
        }

        /// <summary>
        /// 获取所有文件的后缀名
        /// </summary>
        /// <param name="dir">文件夹路径</param>
        /// <returns></returns>
        public static List<string> GetFileExtension(string dir)
        {
            List<string> result = new List<string>();

            if (Directory.Exists(dir))
            {
                DirectoryInfo dirInfo = new DirectoryInfo(dir);
                FileInfo[] fileInfos = dirInfo.GetFiles(".", SearchOption.AllDirectories);
                foreach (var f in fileInfos)
                {
                    result.Add(f.Extension);
                }
            }

            return result.Distinct().ToList();
        }
    }
}
